package cc.mcggdd.leah.boot.stock.service.impl;

import cc.mcggdd.leah.boot.core.security.util.SecurityUtils;
import cc.mcggdd.leah.boot.stock.model.query.StockTransferStatisticQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockTransferMonthRollupVO;
import cc.mcggdd.leah.boot.stock.model.vo.StockTransferStatisticVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.mcggdd.leah.boot.stock.mapper.StockTransferMapper;
import cc.mcggdd.leah.boot.stock.service.StockTransferService;
import cc.mcggdd.leah.boot.stock.model.entity.StockTransfer;
import cc.mcggdd.leah.boot.stock.model.form.StockTransferForm;
import cc.mcggdd.leah.boot.stock.model.query.StockTransferQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockTransferVO;
import cc.mcggdd.leah.boot.stock.converter.StockTransferConverter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 银证转账服务实现类
 *
 * @author zifeng
 * @since 2025-02-07 10:03
 */
@Service
@RequiredArgsConstructor
public class StockTransferServiceImpl extends ServiceImpl<StockTransferMapper, StockTransfer> implements StockTransferService {

    private final StockTransferConverter stockTransferConverter;

    /**
    * 获取银证转账分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<StockTransferVO>} 银证转账分页列表
    */
    @Override
    public IPage<StockTransferVO> getStockTransferPage(StockTransferQuery queryParams) {
        Page<StockTransferVO> pageVO = this.baseMapper.getStockTransferPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    public StockTransferStatisticVO getStockTransferStatistic(StockTransferStatisticQuery queryStatisticParams){
        StockTransferStatisticVO result = this.baseMapper.getStockTransferStatistic(queryStatisticParams);
        return result;
    }

    public List<StockTransferMonthRollupVO> getStockTransferMonthRollup(StockTransferStatisticQuery queryStatisticParams){
        List<StockTransferMonthRollupVO> result = this.baseMapper.getStockTransferMonthRollup(queryStatisticParams);
        return result;
    }
    
    /**
     * 获取银证转账表单数据
     *
     * @param id 银证转账ID
     * @return
     */
    @Override
    public StockTransferForm getStockTransferFormData(Long id) {
        StockTransfer entity = this.getById(id);
        return stockTransferConverter.toForm(entity);
    }
    
    /**
     * 新增银证转账
     *
     * @param formData 银证转账表单对象
     * @return
     */
    @Override
    public boolean saveStockTransfer(StockTransferForm formData) {
        StockTransfer entity = stockTransferConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }
    
    /**
     * 更新银证转账
     *
     * @param id   银证转账ID
     * @param formData 银证转账表单对象
     * @return
     */
    @Override
    public boolean updateStockTransfer(Long id,StockTransferForm formData) {
        StockTransfer entity = stockTransferConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }
    
    /**
     * 删除银证转账
     *
     * @param ids 银证转账ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteStockTransfers(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的银证转账数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public BigDecimal getStockTransferTotalAmount() {
        return this.baseMapper.getStockTransferTotalAmount();
    }

}
